import { defineStore } from "pinia"
import api from "@/api"
import type { ICharacterInfo } from "@/api/info/type"
import { useConfigStore } from "@/store/config"

export interface CharacterInfo extends ICharacterInfo {
  // 基础信息
  calc_token: string
  alter_detail: string
  equ_version: string
}

export const useCharacterStore = defineStore("CharacterInfo", {
  state(): CharacterInfo {
    return {
      alter: "",
      equVersion: "",
      name: "",
      weaopns: [],
      skills: [],
      equips: [],
      calc_token: "",
      alter_detail: "",
      equ_version: "",
      emblems:[],
      enchats: [],
      seal:[]

    }
  },
  getters: {
  },
  actions: {
    async newCharacter(alter: string, equVersion?: string) {
      const configStore = useConfigStore()
      const token = await api.getToken(alter, equVersion)
      configStore.$patch({ token })
      configStore.$patch({ alter })

      Promise.all([api.getCharacter()]).then(async res => {
        this.$patch(res[0])
        await configStore.load()
      })

      this.alter_detail = alter
    },
    async reload() {
      const configStore = useConfigStore()
      configStore.save()
      await this.newCharacter(this.alter_detail, "")
    },
    calc() {
      this.calc_token = new Date().getTime().toString()
    },
    async load() {
      const state = await api.getCharacter()
      this.$patch(state)
    }
  }
})
